package com.lm.scau.dyb.activity.base;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lm.scau.dyb.MainActivity;
import com.lm.scau.dyb.R;
import com.lm.scau.dyb.bean.Movie;
import com.lm.scau.dyb.bean.MovieRole;
import com.lm.scau.dyb.bean.MovieType;
import com.lm.scau.dyb.bean.Notice;
import com.lm.scau.dyb.bean.PackageBean;
import com.lm.scau.dyb.bean.PhotoWall;
import com.lm.scau.dyb.db.Dalx.BaseDalx;
import com.lm.scau.dyb.db.Dalx.MovieDalx;
import com.lm.scau.dyb.db.Dalx.MovieTypeDalx;
import com.lm.scau.dyb.db.Dalx.NoticeDalx;
import com.lm.scau.dyb.db.Dalx.PhotoWallDalx;
import com.lm.scau.dyb.db.Dalx.RoleDalx;
import com.lm.scau.dyb.db.Dalx.TextDalx;
import com.lm.scau.dyb.inter.BasicUIEvent;
import com.lm.scau.dyb.inter.HttpExecuteEvent;
import com.lm.scau.dyb.inter.UICore;
import com.lm.scau.dyb.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/8/20.
 */
public class LoginProcessActivity extends BaseActivity implements HttpExecuteEvent, Handler.Callback, BasicUIEvent {
    private static final int MSG_DOWN_PHOTO_WALL = 10;
    private static final int MSG_DOWN_MOVIE_TYPE = 11;
    private static final int MSG_DOWN_MOVIE_LIST = 12;
    private static final int MSG_DOWN_NOTICE_LIST = 13;
    private static final int MSG_DOWN_TEXT_LIST = 14;
    private static final int MSG_DOWN = 15;
    private static final int MSG_DOWN_ROLE_LIST = 16;

    private static final int BEGIN_PROCESS = 5;
    private static final int MOVIE_PROCESS = 40;
    private static final int TYPE_PROCESS = 15;
    private static final int PHOTO_WALL_PROCESS = 15;
    private static final int NOTICE_PROCESS = 15;
    private static final int ROLE_PROCESS = 10;

    private static final int TEXT_PROCESS = 40;


    private ProgressBar loginProcessBar;
    private TextView processText;
    private Handler handler;
    private int process = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_process);
        loginProcessBar = (ProgressBar) findViewById(R.id.progressbar_login);
        processText = (TextView) findViewById(R.id.process_text);
        handler = new Handler(this);
        setHandelMeeeage("开始获取轮播数据...", BEGIN_PROCESS);
        downLoaddAll();
    }


    private void downLoaddAll() {
        downLoadPhotoWalls();
        downLoadTypes();
        downLoadMovies();
        downNotices();
        downRoles();
      //  downLoadText();
    }

    private void downRoles() {
        String url = getId(new RoleDalx(this), "HomeAction", "getRole");
        httpUtils.httpRequestGet(url, false, this);
    }

    private void downLoadPhotoWalls() {
        String url = getUrl(new PhotoWallDalx(this), "HomeAction", "getPhotoWalls");
        httpUtils.httpRequestGet(url, false, this);
    }

    private void downLoadTypes() {
        String url = getUrl(new MovieTypeDalx(this), "HomeAction", "getMovieType");
        httpUtils.httpRequestGet(url, false, this);
    }

    private void downLoadMovies() {
        String url = getUrl(new MovieDalx(this), "HomeAction", "getMovies");
        httpUtils.httpRequestGet(url, false, this);
    }

    private void downNotices() {
        String url = getUrl(new NoticeDalx(this), "NoticeAction", "getNotices");
        httpUtils.httpRequestGet(url, false, this);
    }

    private void downLoadText() {
        String url = getUrl(new TextDalx(this), "TextAction", "getTexts");
        httpUtils.httpRequestGet(url, false, this);
    }


    /**
     * 获取下载链接
     *
     * @param dalx
     * @param msgname
     * @param method
     * @return
     */
    private String getUrl(BaseDalx dalx, String msgname, String method) {
        String time = dalx.getLatelyTime();
        PackageBean pk = new PackageBean();
        pk.method = method;
        pk.msgname = msgname;
        JSONObject cont = new JSONObject();
        try {
            cont.put("time", time);
            pk.cont = cont;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return pk.toString();
    }


    private String getId(BaseDalx dalx, String msgname, String method) {
        int id = dalx.getId();
        PackageBean pk = new PackageBean();
        pk.method = method;
        pk.msgname = msgname;
        JSONObject cont = new JSONObject();
        try {
            cont.put("ro_id", id);
            pk.cont = cont;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return pk.toString();
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_DOWN:
                String info = msg.obj.toString().split(",")[0];
                int process = Integer.valueOf(msg.obj.toString().split(",")[1]);
                processText.setText(info);
                loginProcessBar.setProgress(process);
                if (100 == process) {
                    mActivity.startActivity(MainActivity.class, null);
                    this.finish();
                }
                break;
            default:
                break;
        }
        return false;
    }


    private void dealResult(String method, JSONObject cont) {
        switch (method) {
            case "getPhotoWalls":
                UICore.eventTask(this, this, MSG_DOWN_PHOTO_WALL, null, cont, false);
                break;
            case "getMovieType":
                UICore.eventTask(this, this, MSG_DOWN_MOVIE_TYPE, null, cont, false);
                break;
            case "getMovies":
                UICore.eventTask(this, this, MSG_DOWN_MOVIE_LIST, null, cont, false);
                break;
            case "getNotices":
                UICore.eventTask(this, this, MSG_DOWN_NOTICE_LIST, null, cont, false);
                break;
            case "getRole":
                UICore.eventTask(this, this, MSG_DOWN_ROLE_LIST, null, cont, false);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResponeSuccess(String respone) {
        try {
            JSONObject obj = new JSONObject(respone);
            String method = obj.getString("method");
            JSONObject cont = obj.getJSONObject("cont");
            dealResult(method, cont);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResponeError(String respone) {
        showToast("未知错误，请重试...");
    }

    private void setHandelMeeeage(String msg, int pro) {
        synchronized (this) {
            process += pro;
        }
        Message message = Message.obtain(handler, MSG_DOWN, msg + String.valueOf(process) + "%," + process);
        message.sendToTarget();
    }

    @Override
    public void execute(int msg, Object obj) {
        switch (msg) {
            case MSG_DOWN_PHOTO_WALL:
                dealPhotoWalls((JSONObject) obj);
                break;
            case MSG_DOWN_MOVIE_TYPE:
                dealTypes((JSONObject) obj);
                break;
            case MSG_DOWN_MOVIE_LIST:
                dealMovies((JSONObject) obj);
                break;
            case MSG_DOWN_NOTICE_LIST:
                dealNotice((JSONObject) obj);
                break;
            case MSG_DOWN_TEXT_LIST:
                //  dealText((JSONObject) obj);
                break;
            case MSG_DOWN_ROLE_LIST:
                  dealRoles((JSONObject) obj);
                break;

            default:
                break;
        }
    }

    private void dealRoles(JSONObject cont) {
        try {
            JSONArray roles = cont.getJSONArray("movieRoles");
            RoleDalx dalx = new RoleDalx(this);
            for (int i = 0; i < roles.length(); i++) {
                JSONObject obj = roles.getJSONObject(i);
                MovieRole role = new MovieRole();
                role.setRo_id(obj.getInt("ro_id"));
                role.setRo_name(obj.getString("ro_name"));
                role.setRo_alpha(obj.getString("ro_alpha"));
                role.setRo_letter(obj.getString("ro_letter"));
                role.setMovie_id(obj.getString("movie_id"));
                role.setRo_url(obj.getString("ro_url"));
                dalx.addMovieRoles(role);
            }
            setHandelMeeeage("主演列表更新成功...", ROLE_PROCESS);
        } catch (JSONException e) {
            showToast("解析主演列表出错...");
        }
    }

    private void dealPhotoWalls(JSONObject cont) {
        try {
            JSONArray photoWalls = cont.getJSONArray("photoWalls");
            PhotoWallDalx dalx = new PhotoWallDalx(this);
            for (int i = 0; i < photoWalls.length(); i++) {
                JSONObject obj = photoWalls.getJSONObject(i);
                PhotoWall pw = new PhotoWall();
                pw.setPhID(obj.getString("phID"));
                pw.setUrl(obj.getString("url"));
                pw.setTime(obj.getString("time"));
                dalx.addPhotoWalls(pw);
            }
            setHandelMeeeage("轮播数据更新成功...", PHOTO_WALL_PROCESS);
        } catch (JSONException e) {
            showToast("解析轮播数据出错...");
        }
    }

    private void dealMovies(JSONObject cont) {
        try {
            JSONArray types = cont.getJSONArray("Movies");
            MovieDalx dalx = new MovieDalx(this);
            for (int i = 0; i < types.length(); i++) {
                JSONObject obj = types.getJSONObject(i);
                Movie mv = new Movie();
                mv.setMoveiID(obj.getInt("moveiID"));
                mv.setMoviceUsing(obj.getString("moviceUsing"));
                mv.setMovieName(obj.getString("movieName"));
                mv.setMoviePhurl(obj.getString("moviePhurl"));
                mv.setMovieRole(obj.getString("movieRole"));
                mv.setMovieSummary(obj.getString("movieSummary"));
                mv.setMovieTime(obj.getString("movieTime"));
                mv.setMovieType(obj.getString("movieType"));
                mv.setMovieUrl(obj.getString("movieUrl"));
                dalx.addMovies(mv);
            }
            setHandelMeeeage("电影列表更新成功...", MOVIE_PROCESS);
        } catch (JSONException e) {
            showToast("解析电影列表出错...");
        }

    }

    private void dealTypes(JSONObject cont) {
        try {
            JSONArray types = cont.getJSONArray("movieTypes");
            MovieTypeDalx dalx = new MovieTypeDalx(this);
            for (int i = 0; i < types.length(); i++) {
                JSONObject obj = types.getJSONObject(i);
                MovieType mt = new MovieType();
                mt.setTyID(obj.getString("tyID"));
                mt.setUrl(obj.getString("url"));
                mt.setTyType(obj.getString("tyType"));
                mt.setTime(obj.getString("time"));
                dalx.addMovieTypes(mt);
            }
            setHandelMeeeage("分类列表更新成功...", TYPE_PROCESS);
        } catch (JSONException e) {
            showToast("解析分类列表出错...");
        }
    }

    private void dealNotice(JSONObject cont) {
        try {
            JSONArray types = cont.getJSONArray("notices");
            NoticeDalx dalx = new NoticeDalx(this);
            for (int i = 0; i < types.length(); i++) {
                JSONObject obj = types.getJSONObject(i);
                Notice nt = new Notice();
                nt.setNoticeID(obj.getString("noticeID"));
                nt.setNoticeInfo(obj.getString("noticeInfo"));
                nt.setNoticeTime(obj.getString("noticeTime"));
                nt.setNoticeTitle(obj.getString("noticeTitle"));
                dalx.addNotices(nt);
            }
            setHandelMeeeage("公告更新成功...", NOTICE_PROCESS);
        } catch (JSONException e) {
            showToast("解析公告出错...");
        }
    }
}
